package com.umeox.watch.moto.gallery.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.watch.themes.util.UToast;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.dialog.ActionOptionsDialogX;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.gallery.R;
import com.umeox.watch.moto.gallery.adapter.PhotoPreviewAdapter;
import com.umeox.watch.moto.gallery.utils.NetworkUtils;

import java.util.ArrayList;


/**
 * Created by zl on 2017/5/18.
 */

public class BigPhotoActivity extends ThemeAppCompatActivity implements ActionOptionsDialogX.OnClickListener, PhotoPreviewAdapter.OnItemLongClickListener {

    private static final String TAG = "BigPhotoActivity";
    public static final String POSITION = "position";
    public static final String PHOTO_LIST = "photo_list";
    public static final String EXTRA_IMAGE_PATH = "imagePath";
    public static final String EXTRA_IMAGE_FRAME = "imageframe";
    private static final int REQUEST_CODE = 101;
    public static final int FRAME_REQUEST_CODE = 0x200;
    private ImageView mOptionIv;

    private int mCurrentPosition;
    private ArrayList<String> mPhotos;

    private ArrayList<String> mPhotoList = new ArrayList<>();

    private static final int[] RES_ID_DESTS = {
            R.string.str_filter,
            R.string.str_share,
            R.string.str_upload,
            R.string.str_delete
    };
    private static final int[] RES_ID_ICONS = {
            R.drawable.gallery_filter_icon,
            R.drawable.gallery_share_icon,
            R.drawable.gallery_upload_icon,
            R.drawable.gallery_delete_icon,
    };
    private PhotoPreviewAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private PagerSnapHelper mPagerSnapHelper;
    private String mCurrentPhotoPath;
    private RecyclerView photoRv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bigphoto);

        if (getIntent().hasExtra(POSITION) && getIntent().hasExtra(PHOTO_LIST)) {
            mCurrentPosition = getIntent().getExtras().getInt(POSITION);
            mPhotos = getIntent().getExtras().getStringArrayList(PHOTO_LIST);
        }

        photoRv = findViewById(R.id.rv_photo_preview);
        photoRv.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        photoRv.setLayoutManager(mLayoutManager);
        mAdapter = new PhotoPreviewAdapter(mPhotos, this);
        mPagerSnapHelper = new PagerSnapHelper();
        mPagerSnapHelper.attachToRecyclerView(photoRv);
        photoRv.setAdapter(mAdapter);
        mOptionIv = findViewById(R.id.iv_options_big);
        mOptionIv.setOnClickListener(v -> showActionDialog());
        photoRv.scrollToPosition(mCurrentPosition);
        final LinearLayoutManager layoutManager = (LinearLayoutManager) photoRv.getLayoutManager();
        if (layoutManager != null) {
            layoutManager.scrollToPositionWithOffset(mCurrentPosition, 0);
        }
    }

    private void showActionDialog() {
        final View view = mPagerSnapHelper.findSnapView(mLayoutManager);
        if (view == null) {
            return;
        }
        mCurrentPosition = mLayoutManager.getPosition(view);
        mPhotoList.clear();
        mCurrentPhotoPath = mPhotos.get(mCurrentPosition);
        mPhotoList.add(mCurrentPhotoPath);
        ActionOptionsDialogX dialog = new ActionOptionsDialogX(this, RES_ID_DESTS, RES_ID_ICONS, this);
        dialog.setOnClickListener(this);
        dialog.show();
        mOptionIv.setVisibility(View.GONE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            mPhotos.remove(mCurrentPosition);
            mAdapter.notifyItemRemoved(mCurrentPosition);
            if (mPhotos.isEmpty()) {
                finish();
            }
        } else if (resultCode == RESULT_OK) {
            if (requestCode == FRAME_REQUEST_CODE && data != null) {
                String imagePath = data.getStringExtra(EXTRA_IMAGE_FRAME);
                mCurrentPosition = mCurrentPosition + 1;
                mPhotos.add(mCurrentPosition, imagePath);
                mAdapter.notifyItemInserted(mCurrentPosition);
                //mViewPager.setCurrentItem(mPhotos.indexOf(imagePath));
                photoRv.scrollToPosition(mCurrentPosition);
            }
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onOption1Click() {
        if (TextUtils.isEmpty(DataProvider.getCustomWallpaper()) || !mCurrentPhotoPath.equals(DataProvider.getCustomWallpaper())) {
            if (DataProvider.setCustomWallpaper(mCurrentPhotoPath)) {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | Constants.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                intent.setAction(Constants.ACTION_REFRESH_WALLPAPERS);
                sendBroadcast(intent);
            }
            UToast.show(getResources().getString(R.string.set_wallpaper_success_str), Toast.LENGTH_LONG);
        }
    }

    @Override
    public void onOption2Click() {
        Intent intent = new Intent(this, MakeFrameActivity.class);
        intent.putExtra(EXTRA_IMAGE_PATH, mPhotos.get(mCurrentPosition));
        startActivityForResult(intent, FRAME_REQUEST_CODE);
    }


    @Override
    public void onOption3Click() {
        Uri uri = Uri.parse("chat://file/" + mCurrentPhotoPath);
        Intent intent = new Intent("com.wherecom.intent.action.CHAT", uri);
        startActivity(intent);
    }

    @Override
    public void onOption4Click() {
        if (!NetworkUtils.isNetworkAvailable(this)) {
            UToast.showCustomToast(this, R.string.sim_state_unknown, 2000);
            return;
        }
        Intent intent = new Intent(BigPhotoActivity.this, UploadActivity.class);
        intent.putStringArrayListExtra("list", mPhotoList);
        startActivity(intent);
    }

    @Override
    public void onOption5Click() {
        Intent intent = new Intent(BigPhotoActivity.this, ConfirmDeleteActivity.class);
        intent.putStringArrayListExtra("list", mPhotoList);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onLongClick(int position) {
        if (mOptionIv.getVisibility() == View.GONE) {
            mOptionIv.setVisibility(View.VISIBLE);
        } else {
            mOptionIv.setVisibility(View.GONE);
        }
    }
}
